package com.springboot.mybatis.handle;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springboot.mybatis.common.ResponseDTO;
import com.springboot.mybatis.common.util.MailUtil;

@ControllerAdvice
public class ExceptionHandle {
     
        private final static Logger logger= LoggerFactory.getLogger(ExceptionHandle.class);
        @Resource
        private MailUtil mailUtil;
        
        @ResponseBody
        @ExceptionHandler(value = Exception.class)
        public Object handle(Exception e){
            logger.error("【系统异常】{}", e);
            mailUtil.sendSimpleMail("542868998@qq.com", "异常", e.getMessage());
            if(e instanceof Exception){
                return new ResponseDTO<String>(e.getMessage());
            }else {
                return new ResponseDTO<String>(e.getMessage());
            }
        }

}
